package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.GeRen_Main_Fans_ListView_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToggleButton;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class Visit_JiLu_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private Button btn_gerentg_tgjl, btn_gerentg_ddjl, btn_gerentg_fwsq;
    private MyListView lv_xyf_jilu;
    private TextView tv_gerentg_jilu_title, tv_wallet_withdraw_limit_money, tv_wallet_withdraw_limit_money_yb;
    private String access_token;
    private LinearLayout ll_visit_1, ll_visit_2;
    private ToggleButton tb_shaixuan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visit_jilu);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        tv_gerentg_jilu_title = findViewById(R.id.tv_gerentg_jilu_title);
        tv_wallet_withdraw_limit_money = findViewById(R.id.tv_wallet_withdraw_limit_money);
        tv_wallet_withdraw_limit_money_yb = findViewById(R.id.tv_wallet_withdraw_limit_money_yb);
        tb_shaixuan = findViewById(R.id.tb_shaixuan);
        ll_visit_1 = findViewById(R.id.ll_visit_1);
        ll_visit_2 = findViewById(R.id.ll_visit_2);
        lv_xyf_jilu = findViewById(R.id.lv_xyf_jilu);
        btn_gerentg_tgjl = findViewById(R.id.btn_gerentg_tgjl);
        btn_gerentg_ddjl = findViewById(R.id.btn_gerentg_ddjl);
        btn_gerentg_fwsq = findViewById(R.id.btn_gerentg_fwsq);
        back.setOnClickListener(this);
        btn_gerentg_tgjl.setOnClickListener(this);
        btn_gerentg_ddjl.setOnClickListener(this);
        btn_gerentg_fwsq.setOnClickListener(this);
        postUserData();
        postData(1);
        tb_shaixuan.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
//                    ToolUtils.showToast(this, "聊天已置顶");
                } else {
//                    ToolUtils.showToast(this, "聊天已关闭");
                }
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_gerentg_tgjl:
                ll_visit_1.setVisibility(View.VISIBLE);
                ll_visit_2.setVisibility(View.GONE);
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_login);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_exit);
                btn_gerentg_fwsq.setBackgroundResource(R.drawable.login_exit);
                postData(1);
                break;
            case R.id.btn_gerentg_ddjl:
                ll_visit_1.setVisibility(View.GONE);
                ll_visit_2.setVisibility(View.VISIBLE);
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_exit);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_login);
                btn_gerentg_fwsq.setBackgroundResource(R.drawable.login_exit);
                postData(2);
                break;
            case R.id.btn_gerentg_fwsq:
                ll_visit_1.setVisibility(View.GONE);
                ll_visit_2.setVisibility(View.GONE);
                btn_gerentg_tgjl.setBackgroundResource(R.drawable.login_exit);
                btn_gerentg_ddjl.setBackgroundResource(R.drawable.login_exit);
                btn_gerentg_fwsq.setBackgroundResource(R.drawable.login_login);
                postData(3);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(int type) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.main_User_list_Url + access_token + "&limit=10&page=1&type=" + type, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        List<User_Limit> mlist = gson.fromJson(info.toString(), new TypeToken<List<User_Limit>>(){}.getType());
                        Log.d("xinyun", JSON.toJSONString(mlist));
                        lv_xyf_jilu.setAdapter(new GeRen_Main_Fans_ListView_Adapter(Visit_JiLu_Activity.this, mlist, 2, type));
                    } else {
                        ToolUtils.showToast(Visit_JiLu_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Visit_JiLu_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postUserData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.main_visit_jilu_Url + access_token, null, new Response.Listener<JSONObject>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        String visit_count = info.getString("visit_count");
                        String to_visit_count = info.getString("to_visit_count");
                        int is_no_visit = info.getInt("is_no_visit");
                        Log.d("xinyun", visit_count);
                        tv_wallet_withdraw_limit_money.setText("" + visit_count);
                        tv_wallet_withdraw_limit_money_yb.setText("" + to_visit_count);
                        if (is_no_visit == 0){ //是否是无痕访问 1是 0不是
                            tb_shaixuan.setToggleOff();
                        } else {
                            tb_shaixuan.setToggleOn();
                        }
                    } else {
                        ToolUtils.showToast(Visit_JiLu_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Visit_JiLu_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
